package ynu.lcy.service.impl;


import org.springframework.stereotype.Service;
import ynu.lcy.dao.IFoodDao;
import ynu.lcy.dto.food.AddFoodRequestDTO;
import ynu.lcy.elememicroserviceserviceclient.service.BusinessFeignClient;
import ynu.lcy.elememicroserviceserviceclient.service.FileUploadFeignClient;
import ynu.lcy.entity.Business;
import ynu.lcy.entity.Food;
import ynu.lcy.service.FoodService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Resource
    private IFoodDao dao;

    @Resource
    private BusinessFeignClient businessFeignClient;

    @Resource
    private FileUploadFeignClient uploadFile;

    /**
     * 根据商家id获取商品
     * @param id
     * @return
     * */
    public List<Food> getByBusinessId(Long id){
        Business business = businessFeignClient.getById(id);
        return dao.findAllByBusinessId(business.getId());
    }

    /**
     * 获取食品
     *  @param id id
     *  @return Food
     * */
    public Food getById(Long id){
        return dao.getReferenceById(id);
    }

    public List<Food> getAll(){
        return  dao.findAll();
    }

    /**
     * 文件上传
     *  @param dto dto
     *  @return Food
     * */
    public Food addFood(AddFoodRequestDTO dto) {
        String file = uploadFile.upload(dto);
        Food food = new Food().create(dto, file);
        return dao.save(food);
    }

    /**
     * 删除商品
     *
     * @param id id
     * @return
     */
    public Boolean delete(Long id) {
        try {
            Food food = dao.getReferenceById(id);
            food.setIsDeleted(1);
            dao.save(food);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
